package com.ihr360.payroll.service.salarytasklet.listener;

import com.ihr360.job.core.listener.ItemListenerSupport;
import com.ihr360.job.core.repository.JobRepository;
import com.ihr360.job.core.scope.context.SimpleStepContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
/**
 * @author davidwei
 */
@Component
public class ItemFailureLoggerListener extends ItemListenerSupport {
    private static final Logger logger = LoggerFactory.getLogger(ItemFailureLoggerListener.class.getName());

    @Autowired
    private JobRepository jobRepository;

    @Override
    public void onReadError(SimpleStepContext simpleStepContext, Exception ex) {
        jobRepository.error(simpleStepContext,"["+simpleStepContext.getStepName()+"]","读数据发生异常中断");
        logger.error(ex.getMessage(), ex);
    }

    @Override
    public void onWriteError(Exception ex, List item) {
        logger.error(ex.getMessage(), ex);
    }

    @Override
    public void onProcessError(Object item, SimpleStepContext simpleStepContext, Exception e) {
        jobRepository.error(simpleStepContext,"["+simpleStepContext.getStepName()+"]","处理过程中发生异常");
        logger.error(e.getMessage(), e);
    }
}